﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem
{
    public partial class ListProduct : System.Web.UI.Page
    {
        public static int catID;
        public static int sizePage;
        public static int currentPage;
      
        public string catName;
        public static DataTable dt;
        public static int AllCat;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                if (SaleOnlineSystem.Controls.SortProduct.column == null) SaleOnlineSystem.Controls.SortProduct.column = "Product_Name";
                if (SaleOnlineSystem.Controls.SortProduct.sort == null) SaleOnlineSystem.Controls.SortProduct.sort = "DESC";

                    sizePage = Convert.ToInt32(ConfigurationManager.AppSettings["sizepage"].ToString());
                    

                    if (Request.QueryString["currentPage"] != null)
                    {
                        if (Request.QueryString["catID"] != null)
                            catID = Convert.ToInt32(Request.QueryString["catID"].ToString());
                        else
                            Response.Redirect("Error.aspx");



                        currentPage = Convert.ToInt32(Request.QueryString["currentPage"].ToString());
                        
                        dt = BOL.ProductManager.Instance.GetListProductsPaging("tbl_Product", "paging", currentPage, sizePage, catID,
                                                                                SaleOnlineSystem.Controls.SortProduct.column, SaleOnlineSystem.Controls.SortProduct.sort);
                    }


                    else
                    {
                        if (Request.QueryString["catID"] != null)
                            catID = Convert.ToInt32(Request.QueryString["catID"].ToString());
                        else
                            Response.Redirect("Error.aspx");


                        dt = BOL.ProductManager.Instance.GetListProductsPaging("tbl_Product", "paging", 1, sizePage, catID,
                                                                              SaleOnlineSystem.Controls.SortProduct.column, SaleOnlineSystem.Controls.SortProduct.sort);
                        currentPage = 1;
                    }

                    if (SaleOnlineSystem.Controls.QuickSearch.vendors != null && SaleOnlineSystem.Controls.QuickSearch.vendors != "null")
                    {

                        DataView dv = dt.DefaultView;
                        dv.RowFilter = "Vendor_ID IN (" + SaleOnlineSystem.Controls.QuickSearch.vendors + ")";
                        dt = dv.ToTable();
                    }
                    // VIet stored dem so san pham co catID = CatID
                    if (SaleOnlineSystem.Controls.QuickSearch.vendors != null && SaleOnlineSystem.Controls.QuickSearch.vendors != "null")
                    {

                        DataView dv = dt.DefaultView;
                        dv.RowFilter = "Vendor_ID IN (" + SaleOnlineSystem.Controls.QuickSearch.vendors + ")";
                        dt = dv.ToTable();
                    }
                    if (SaleOnlineSystem.Controls.QuickSearch.price != null && SaleOnlineSystem.Controls.QuickSearch.price != "null")
                    {
                        DataView dv = dt.DefaultView;
                        dv.RowFilter = SaleOnlineSystem.Controls.QuickSearch.price;
                        dt = dv.ToTable();
                        SaleOnlineSystem.Controls.QuickSearch.price = null;
                    }
                    string name = DAL.DCollector.DBCollector.Instance.StoredProcedureGetTableByID("tbl_Product", catID,
                                                                                                  "selectcatname").Rows[0]["catname"].ToString();

                    AllCat = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Product", name, catID, "selectcountwhere").Rows[0]["count"].ToString());

                    catName = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID1", catID, "selectwhereid").Rows[0]["Category_Name"].ToString();
                
            }

        }
    }
}